﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Speech.Recognition;
using System.Text;

namespace CounterStrikeSourceAimbot.Speech
{
    public class LiveSpeechRecognizer
    {
        public event EventHandler<SpeechRecognizedEventArgs> SpeechRecognized;

        public void Start()
        {
            SpeechRecognitionEngine _speechRecognitionEngine;
            Grammar dictation = new DictationGrammar();
            dictation.Name = "default dictation";

            _speechRecognitionEngine = new SpeechRecognitionEngine();
            _speechRecognitionEngine.LoadGrammar(dictation);
            _speechRecognitionEngine.BabbleTimeout = TimeSpan.FromSeconds(0.50);
            _speechRecognitionEngine.EndSilenceTimeout = TimeSpan.FromSeconds(0.50);
            _speechRecognitionEngine.EndSilenceTimeoutAmbiguous = TimeSpan.FromSeconds(0.50);
            _speechRecognitionEngine.InitialSilenceTimeout = TimeSpan.FromSeconds(0.50);

            _speechRecognitionEngine.RecognizeCompleted += SpeechRecognitionEngineOnRecognizeCompleted;
            _speechRecognitionEngine.SpeechRecognized+=SpeechRecognitionEngineOnSpeechRecognized;

            _speechRecognitionEngine.SetInputToDefaultAudioDevice();
            var inf = _speechRecognitionEngine.RecognizerInfo;
            _speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
        }

        private void SpeechRecognitionEngineOnSpeechRecognized(object sender, System.Speech.Recognition.SpeechRecognizedEventArgs e)
        {
            if (SpeechRecognized != null)
            {
                SpeechRecognized(this, new SpeechRecognizedEventArgs(new SpeechReconitionResult(){Text=e.Result.Text}));
            }
        }

        private void SpeechRecognitionEngineOnRecognizeCompleted(object sender, RecognizeCompletedEventArgs e)
        {
            if (SpeechRecognized != null)
            {
                SpeechRecognized(this,new SpeechRecognizedEventArgs());
            }
        }
    }

    /*
    public class SpeechReconizer
    {
        SpeechRecognitionEngine _speechRecognitionEngine;
        public SpeechReconitionResult ReadResult { get; set; }

        public SpeechReconizer()
        {
            Grammar dictation = new DictationGrammar();
            dictation.Name = "Dictation Grammar";

            _speechRecognitionEngine = new SpeechRecognitionEngine();
            _speechRecognitionEngine.LoadGrammar(dictation);
            _speechRecognitionEngine.BabbleTimeout = TimeSpan.FromSeconds(10.0);
            _speechRecognitionEngine.EndSilenceTimeout = TimeSpan.FromSeconds(10.0);
            _speechRecognitionEngine.EndSilenceTimeoutAmbiguous = TimeSpan.FromSeconds(10.0);
            _speechRecognitionEngine.InitialSilenceTimeout = TimeSpan.FromSeconds(10.0);

            _speechRecognitionEngine.SpeechRecognized += RecognizerSpeechRecognized;
            _speechRecognitionEngine.RecognizeCompleted += RecognizerRecognizeCompleted;
        }



        public SpeechReconitionResult ReadSpeech(string sourceAudio)
        {
            ReadResult = new SpeechReconitionResult();

            _speechRecognitionEngine.SetInputToWaveFile(sourceAudio);

            _speechRecognitionEngine.SetInputToDefaultAudioDevice();

            _speechRecognitionEngine.Recognize();
            return ReadResult;

        }

        public SpeechReconitionResult ReadSpeechFromMic()
        {
            ReadResult = new SpeechReconitionResult();

            //_speechRecognitionEngine.SetInputToWaveFile(sourceAudio);

            _speechRecognitionEngine.SetInputToDefaultAudioDevice();

            _speechRecognitionEngine.Recognize();
            
            return ReadResult;

        }

        private void RecognizerSpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if (e.Result != null && e.Result.Text != null)
            {
                ReadResult.Success = true;
                ReadResult.Text = e.Result.Text;
            }
            else
            {
                ReadResult.Text = "Recognized text not available.";
            }
        }

        private void RecognizerRecognizeCompleted(object sender, RecognizeCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                ReadResult.Success = false;
                ReadResult.ErrorMessage = string.Format("{0}: {1}",
                              e.Error.GetType().Name, e.Error.Message);
            }
            if (e.Cancelled)
            {
                ReadResult.Success = false;
                ReadResult.ErrorMessage = "Operation cancelled.";
            }
        }
    }
    
    */

    public class SpeechRecognizedEventArgs:EventArgs
    {
        public SpeechReconitionResult Result;

        public SpeechRecognizedEventArgs(SpeechReconitionResult result)
        {
            Result = result;
        }

        public SpeechRecognizedEventArgs()
        {
        }
    }

    public class SpeechReconitionResult
    {
        public string Text { get; set; }
        public bool Success { get; set; }
        public string ErrorMessage { get; set; }
        public bool Complete { get; set; }
    }


}
